import java.util.Scanner;

/*2. 编程题

        编程获取两个指定字符串中的最大相同子串。

        如： s1="asdafghjka", s2="aaasdfg" 他们的最大子串为"asd"*/
public class Q2 {
    public static String[] smallTos1(String s1, String s2) {
        return s1.length() <= s2.length() ? new String[]{s1, s2} : new String[]{s2, s1};
    }


    public static String findLongestCommon(String s1, String s2) {
        //短字符串放s1
        String[] stringArr = smallTos1(s1, s2);
        s1 = stringArr[0];
        s2 = stringArr[1];
        //以S1为参照 长度sublength逐次降低，枚举所有长sublength的子串，判断s2是否包含，有则返回子串，否则返回null
        for (int sublength = s1.length(); sublength > 0; sublength--) {
            for (int start_idx = 0; start_idx <= s1.length() - sublength; start_idx++) {
                String subString = s1.substring(start_idx, start_idx + sublength);
                if (s2.contains(subString)) {
                    return subString;
                }
            }
        }
        return null;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("请输入第一个字符串：");
            String s1 = sc.next();
            System.out.print("请输入第二个字符串：");
            String s2 = sc.next();
            //System.out.println(Arrays.toString(smallTos1(s1, s2)));
            String result = findLongestCommon(s1, s2);
            if (result != null) {
                System.out.println("公共最大子串是：" + result);
            } else System.out.println("没有公共子串");
        }
        //sc.close();

    }
}
// asdafghjka  aaasdfg  ---> asd
// 123456      123456   ---> 123456
// 111123      11211112 ---> 11112
// asdfgadfa   [poipoiua ---> a
// asdf        ;lkj      ---> 没有公共子串